package com.nexgo.payment.trans.download;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.nexgo.common.ByteUtils;
import com.nexgo.data.entity.RecvData;
import com.nexgo.payment.App;
import com.nexgo.payment.R;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.TransType;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.trans.AbsOnlinePresenter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 非接参数操作业务
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年07月12日
 */
public class RfParamPresenter extends AbsOnlinePresenter implements DownloadTransContract.Presenter {
    private Logger mLog = LoggerFactory.getLogger(RfParamPresenter.class.getSimpleName());

    private DownloadTransContract.View mView;

    public RfParamPresenter(@NonNull DownloadTransContract.View view, @NonNull DataSource repository) {
        super(view, repository);
        mView = checkNotNull(view);
        mView.setPresenter(this);
    }

    @Override
    protected TransData getTransData() {
        TransData transData = new TransData();
        transData.setTransType(TransType.RF_PARAM_DOWN);
        transData.setTemporary("01");
        return transData;
    }

    @Override
    protected void onFirstAction() {
        postMainThread(() -> mView.showOnlineProc(mTransData, App.sContext.getString(R.string.rf_param_down)));
    }

    @Override
    protected void onTransSuccess(RecvData recvData) {
        if (TextUtils.isEmpty(recvData.getField60())) {
            postMainThread(() -> mView.showTransFailed());
            return;
        }
//        wzp add
        String param = new String(ByteUtils.hexString2ByteArray(recvData.getField60())).toUpperCase();
        int len;
        int offset = 0 ;
        int idx;
        String value;

        //非接快速业务标识
        value = param.substring(offset, offset+1);
        mLog.debug("非接快速业务标识 {}",value);
        mRepository.setParamValue(PreferencesConstants.QPS_SUPPORT_FREE_PASSWORD, Integer.parseInt(value) > 0 ? "1" : "0");
        offset ++;
        //BIN表A标识
        value = param.substring(offset, offset+1);
        mLog.debug("BIN表A标识 {}",value);
        mRepository.setParamValue(PreferencesConstants.QPS_SUPPORT_CARD_BIN_A, Integer.parseInt(value) > 0 ? "1" : "0");
        offset ++;

        //BIN表B标识
        value = param.substring(offset, offset+1);
        mLog.debug("BIN表B标识 {}",value);
        mRepository.setParamValue(PreferencesConstants.QPS_SUPPORT_CARD_BIN_B, Integer.parseInt(value) > 0 ? "1" : "0");
        offset ++;


        //CDCVM标识
        value = param.substring(offset, offset+1);
        mLog.debug("CDCVM标识 {}",value);
        mRepository.setParamValue(PreferencesConstants.QPS_SUPPORT_CDCVM, Integer.parseInt(value) > 0 ? "1" : "0");
        offset ++;

        //免签标识
        value = param.substring(offset, offset+1);
        mLog.debug("免签标识 {}",value);
        mRepository.setParamValue(PreferencesConstants.QPS_SUPPORT_FREE_SIGNATURE, Integer.parseInt(value) > 0 ? "1" : "0");
        offset ++;

//        免密限额
        value = param.substring(offset, offset+12);
        mLog.debug("免密限额 {}",value);
        mRepository.setParamValue(PreferencesConstants.QPS_LIMIT_OF_FREE_PASSWORD, value);
        offset += 12 ;

        //非接快速业务标识
        value = param.substring(offset, offset+1);

        mLog.debug("非接快速业务标识 {}",value);
        mRepository.setParamValue(PreferencesConstants.QPS_SUPPORT_FREE_PASSWORD, Integer.parseInt(value) > 0 ? "1" : "0");
        offset ++;

//        免签限额
        value = param.substring(offset, offset+12);
        mLog.debug("免签限额 {}",value);
        mRepository.setParamValue(PreferencesConstants.QPS_LIMIT_OF_FREE_SIGNATURE, value);

        mRepository.syncParamValue();
        postMainThread(() -> mView.showTransSuccess());
    }
}
